package net.csdn.business.discuss.api.service.impl;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import net.csdn.business.discuss.api.config.ApiConfigData;
import net.csdn.business.discuss.api.model.dto.*;
import net.csdn.business.discuss.api.model.vo.DiscussCommentVO;
import net.csdn.business.discuss.api.model.vo.DiscussVO;
import net.csdn.business.discuss.api.service.IDiscussActivityService;
import net.csdn.business.discuss.api.service.IDiscussCommentService;
import net.csdn.business.discuss.api.service.IDiscussService;
import net.csdn.business.discuss.api.service.INotifyService;
import net.csdn.business.discuss.common.constant.NotifyConstants;
import net.csdn.business.discuss.common.enums.DiscussStateEnum;
import net.csdn.business.discuss.common.enums.SourceTypeEnum;
import net.csdn.business.discuss.common.model.dto.OrgMemberIdDTO;
import net.csdn.business.discuss.common.model.dto.SendNotifyDTO;
import net.csdn.business.discuss.common.model.vo.OrgInfoVo;
import net.csdn.business.discuss.common.model.vo.ProjectVo;
import net.csdn.business.discuss.common.model.vo.UserInfoVO;
import net.csdn.business.discuss.common.utils.HtmlParserTextUtil;
import net.csdn.business.discuss.feign.api.NotifyFeignClient;
import net.csdn.business.discuss.feign.api.OrgFeignClient;
import net.csdn.business.discuss.feign.api.ProjectFeignClient;
import net.csdn.business.discuss.feign.api.UserFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @ClassName NotifyServiceImpl
 * @Description TODO
 * @Author zhangyl
 * @Date 2024/1/3 14:46
 */
@Slf4j
@Service
public class NotifyServiceImpl implements INotifyService {

    @Autowired
    private NotifyFeignClient notifyFeignClient;
    @Autowired
    private OrgFeignClient orgFeignClient;
    @Autowired
    private UserFeignClient userFeignClient;
    @Autowired
    private ProjectFeignClient projectFeignClient;
    @Autowired
    private IDiscussService discussService;
    @Autowired
    private ApiConfigData apiConfigData;
    @Autowired
    private IDiscussActivityService discussActivityService;
    @Autowired
    private IDiscussCommentService discussCommentService;

    public void sendDiscussCreate(DiscussCreateActivityDTO params) {

        String currUserId=params.getActionUid();
        String discussId=params.getDiscussId();
        log.info("send discuss create notify begin discussId:{}",discussId);
        DiscussVO discussVO=discussService.selectDiscussById(discussId);
        if(discussVO==null){
            log.info("send discuss create notify cancle ,discussId:{} is not exist",discussId);
            return;
        }
        Set<String>  commentAtsUserIds=null;
        List<String> commentAts=params.getCommentAts();
        if(commentAts!=null&&commentAts.size()>0){
            //批量查询艾特的人
            commentAtsUserIds=userFeignClient.batchQueryByUsername(commentAts);
        }
        Map<String, String> variables=new HashMap<>();
        String title=discussVO.getTitle();
        variables.put("title",title);
        Integer serialNumber=discussVO.getSerialNumber();
        variables.put("serial_num",serialNumber.toString());
        String desc= HtmlParserTextUtil.htmlParserText(discussVO.getContent(),200);
        variables.put("content",desc);
        String sourceId=discussVO.getSourceId();
        Integer sourceType=discussVO.getSourceType();
        //查询用户信息
        List<String> userIds=new ArrayList<>();
        userIds.add(currUserId);
        Map<String, UserInfoVO>  userInfoVOMap= userFeignClient.batchQuery(userIds);
        if(userInfoVOMap!=null){
            UserInfoVO userInfoVO=userInfoVOMap.get(currUserId);
            if(userInfoVO!=null){
                variables.put("nick_name",userInfoVO.getNickname());
            }else{
                variables.put("nick_name","账号已注销");
            }
        }
        //根据discussId查询讨论详情
        SendNotifyDTO notifyParams=new SendNotifyDTO();
        JSONObject nameSpace=new JSONObject();
        nameSpace.put("id",sourceId);
        nameSpace.put("type", SourceTypeEnum.get(sourceType).getCode());
        StringBuilder url=new StringBuilder();
        url.append(apiConfigData.getAtomGitHost());
        Set<String> receiver=new HashSet<>();
        switch (sourceType){
            case 1:
                OrgInfoVo orgInfoVo=orgFeignClient.getById(sourceId);
                if(orgInfoVo==null){
                    log.info("send discuss create notify cancle,org is null");
                    return;
                }
                String path=orgInfoVo.getNameSpace();
                nameSpace.put("path",path);
                nameSpace.put("name",orgInfoVo.getOrgName());
                url.append("/orgs/").append(path);
                Integer page=1;
                long total=0L;
                List<String> pageList =null;
                do {
                    try {
                        //查询组织成员
                        OrgMemberIdDTO query = new OrgMemberIdDTO();
                        query.setOrgId(sourceId);
                        query.setPageNum(page);
                        query.setPageSize(50);
                        Page<String> pageRes = orgFeignClient.getOrgMemberId(query);
                        pageList = pageRes.getRecords();
                        total=pageRes.getTotal();
                        receiver.addAll(pageList);
                        ++page;
                    }catch (Exception e){
                        e.printStackTrace();
                        log.info("send discuss create notify query org member error !");
                    }
                }while (receiver.size()<total);
                break;
            case 2:
                ProjectVo projectVo=projectFeignClient.getProjectById(Long.parseLong(sourceId));
                if(projectVo==null){
                    log.info("send discuss create notify cancle,project is null");
                    return;
                }
                String repoPath=projectVo.getPathWithNamespace();
                nameSpace.put("path",repoPath);
                nameSpace.put("name",projectVo.getNameWithNamespace());
                url.append("/").append(repoPath);
                List<String> repoMember=projectFeignClient.getRepoMemberIds(sourceId);
                receiver.addAll(repoMember);
        }
        url.append("/discussions/").append(serialNumber);
        notifyParams.setUrl(url.toString());
        notifyParams.setNameSpace(nameSpace);
        notifyParams.setSender(currUserId);
        notifyParams.setTargetId(discussId);
        notifyParams.setVariables(variables);
        if(commentAtsUserIds!=null&&commentAtsUserIds.size()>0){
            receiver.removeAll(commentAtsUserIds);
            commentAtsUserIds.remove(currUserId);//排除自己艾特自己
            //发送艾特通知
            notifyParams.setBusCode(NotifyConstants.DISCUSS_REFER);
            notifyParams.setBatchNum(IdUtil.objectId());
            notifyParams.setReceiver(commentAtsUserIds);
            log.info("----send discuss create notify refer params:{}", JSON.toJSONString(notifyParams));
            notifyFeignClient.sendNotify(notifyParams);
        }
        //排除自己
        receiver.remove(currUserId);
        if(receiver.size()>0){
            //移除当前操作人，对本人不通知
            notifyParams.setBusCode(NotifyConstants.DISCUSS_CREATE);
            notifyParams.setBatchNum(IdUtil.objectId());
            notifyParams.setReceiver(receiver);
            log.info("----send discuss create notify params:{}", JSON.toJSONString(notifyParams));
            notifyFeignClient.sendNotify(notifyParams);
        }

    }

    @Override
    public void sendDiscussUpdate(BaseActivityDTO params) {
        String currUserId=params.getActionUid();
        String discussId=params.getDiscussId();
        log.info("send discuss update notify begin discussId:{}",discussId);
        DiscussVO discussVO=discussService.selectDiscussById(discussId);
        if(discussVO==null){
            log.info("send discuss update notify cancle ,discussId:{} is not exist",discussId);
            return;
        }
        Set<String>  commentAtsUserIds=null;
        List<String> commentAts=params.getCommentAts();
        if(commentAts!=null&&commentAts.size()>0){
            //批量查询艾特的人
            commentAtsUserIds=userFeignClient.batchQueryByUsername(commentAts);
        }
        Map<String, String> variables=new HashMap<>();
        String title=discussVO.getTitle();
        variables.put("title",title);
        Integer serialNumber=discussVO.getSerialNumber();
        variables.put("serial_num",serialNumber.toString());
        String desc= HtmlParserTextUtil.htmlParserText(discussVO.getContent(),200);
        variables.put("content",desc);
        String sourceId=discussVO.getSourceId();
        Integer sourceType=discussVO.getSourceType();
        //查询用户信息
        List<String> userIds=new ArrayList<>();
        userIds.add(currUserId);
        Map<String, UserInfoVO>  userInfoVOMap= userFeignClient.batchQuery(userIds);
        if(userInfoVOMap!=null){
            UserInfoVO userInfoVO=userInfoVOMap.get(currUserId);
            if(userInfoVO!=null){
                variables.put("nick_name",userInfoVO.getNickname());
            }else{
                variables.put("nick_name","账号已注销");
            }
        }
        //根据discussId查询讨论详情
        SendNotifyDTO notifyParams=new SendNotifyDTO();
        JSONObject nameSpace=new JSONObject();
        nameSpace.put("id",sourceId);
        nameSpace.put("type", SourceTypeEnum.get(sourceType).getCode());
        StringBuilder url=new StringBuilder();
        url.append(apiConfigData.getAtomGitHost());
        Set<String> receiver=new HashSet<>();
        switch (sourceType){
            case 1:
                OrgInfoVo orgInfoVo=orgFeignClient.getById(sourceId);
                if(orgInfoVo==null){
                    log.info("send discuss update notify cancle,org is null");
                    return;
                }
                String path=orgInfoVo.getNameSpace();
                nameSpace.put("path",path);
                nameSpace.put("name",orgInfoVo.getOrgName());
                url.append("/orgs/").append(path);
                Integer page=1;
                long total=0L;
                List<String> pageList =null;
                do {
                    try {
                        //查询组织成员
                        OrgMemberIdDTO query = new OrgMemberIdDTO();
                        query.setOrgId(sourceId);
                        query.setPageNum(page);
                        query.setPageSize(50);
                        Page<String> pageRes = orgFeignClient.getOrgMemberId(query);
                        pageList = pageRes.getRecords();
                        total=pageRes.getTotal();
                        receiver.addAll(pageList);
                        ++page;
                    }catch (Exception e){
                        e.printStackTrace();
                        log.info("send discuss update notify query org member error !");
                    }
                }while (receiver.size()<total);
                break;
            case 2:
                ProjectVo projectVo=projectFeignClient.getProjectById(Long.parseLong(sourceId));
                if(projectVo==null){
                    log.info("send discuss update notify cancle,project is null");
                    return;
                }
                String repoPath=projectVo.getPathWithNamespace();
                nameSpace.put("path",repoPath);
                nameSpace.put("name",projectVo.getNameWithNamespace());
                url.append("/").append(repoPath);
                List<String> repoMember=projectFeignClient.getRepoMemberIds(sourceId);
                receiver.addAll(repoMember);
        }
        url.append("/discussions/").append(serialNumber);
        notifyParams.setUrl(url.toString());
        notifyParams.setNameSpace(nameSpace);
        notifyParams.setSender(currUserId);
        notifyParams.setTargetId(discussId);
        notifyParams.setVariables(variables);
        //查询此讨论关联人员
        List<String> relateUserId=discussActivityService.selectRelateUser(discussId);
        receiver.addAll(relateUserId);
        if(commentAtsUserIds!=null&&commentAtsUserIds.size()>0){
            receiver.removeAll(commentAtsUserIds);
            commentAtsUserIds.remove(currUserId);//排除自己艾特自己
            //发送艾特通知
            notifyParams.setBusCode(NotifyConstants.DISCUSS_REFER);
            notifyParams.setBatchNum(IdUtil.objectId());
            notifyParams.setReceiver(commentAtsUserIds);
            log.info("----send discuss update notify refer params:{}", JSON.toJSONString(notifyParams));
            notifyFeignClient.sendNotify(notifyParams);
        }
        //排除自己
        receiver.remove(currUserId);
        if(receiver.size()>0){
            //移除当前操作人，对本人不通知
            notifyParams.setBusCode(NotifyConstants.DISCUSS_UPDATE);
            notifyParams.setBatchNum(IdUtil.objectId());
            notifyParams.setReceiver(receiver);
            log.info("----send discuss update notify params:{}", JSON.toJSONString(notifyParams));
            notifyFeignClient.sendNotify(notifyParams);
        }
    }

    @Override
    public void sendDiscussCommentCreate(CommentCreateActivityDTO params) {

        String currUserId=params.getActionUid();
        String discussId=params.getDiscussId();
        log.info("send discuss comment create notify begin discussId:{}",discussId);
        DiscussVO discussVO=discussService.selectDiscussById(discussId);
        if(discussVO==null){
            log.info("send discuss comment create notify cancle ,discussId:{} is not exist",discussId);
            return;
        }
        String commentId=params.getCommentId();
        DiscussCommentVO discussCommentVO=discussCommentService.selectDiscussCommentById(commentId);
        if(discussCommentVO==null){
            return;
        }
        Set<String>  commentAtsUserIds=null;
        List<String> commentAts=params.getCommentAts();
        if(commentAts!=null&&commentAts.size()>0){
            //批量查询艾特的人
            commentAtsUserIds=userFeignClient.batchQueryByUsername(commentAts);
        }
        Map<String, String> variables=new HashMap<>();
        String title=discussVO.getTitle();
        variables.put("title",title);
        Integer serialNumber=discussVO.getSerialNumber();
        variables.put("serial_num",serialNumber.toString());
        String desc= HtmlParserTextUtil.htmlParserText(discussCommentVO.getContent(),200);
        variables.put("comment",desc);
        String sourceId=discussVO.getSourceId();
        Integer sourceType=discussVO.getSourceType();
        //查询用户信息
        List<String> userIds=new ArrayList<>();
        userIds.add(currUserId);
        Map<String, UserInfoVO>  userInfoVOMap= userFeignClient.batchQuery(userIds);
        if(userInfoVOMap!=null){
            UserInfoVO userInfoVO=userInfoVOMap.get(currUserId);
            if(userInfoVO!=null){
                variables.put("nick_name",userInfoVO.getNickname());
            }else{
                variables.put("nick_name","账号已注销");
            }
        }
        //根据discussId查询讨论详情
        SendNotifyDTO notifyParams=new SendNotifyDTO();
        JSONObject nameSpace=new JSONObject();
        nameSpace.put("id",sourceId);
        nameSpace.put("type", SourceTypeEnum.get(sourceType).getCode());
        StringBuilder url=new StringBuilder();
        url.append(apiConfigData.getAtomGitHost());
        Set<String> receiver=new HashSet<>();
        switch (sourceType){
            case 1:
                OrgInfoVo orgInfoVo=orgFeignClient.getById(sourceId);
                if(orgInfoVo==null){
                    log.info("send discuss comment create notify cancle,org is null");
                    return;
                }
                String path=orgInfoVo.getNameSpace();
                nameSpace.put("path",path);
                nameSpace.put("name",orgInfoVo.getOrgName());
                url.append("/orgs/").append(path);
                Integer page=1;
                long total=0L;
                List<String> pageList =null;
                do {
                    try {
                        //查询组织成员
                        OrgMemberIdDTO query = new OrgMemberIdDTO();
                        query.setOrgId(sourceId);
                        query.setPageNum(page);
                        query.setPageSize(50);
                        Page<String> pageRes = orgFeignClient.getOrgMemberId(query);
                        pageList = pageRes.getRecords();
                        total=pageRes.getTotal();
                        receiver.addAll(pageList);
                        ++page;
                    }catch (Exception e){
                        e.printStackTrace();
                        log.info("send discuss comment create notify query org member error !");
                    }
                }while (receiver.size()<total);
                break;
            case 2:
                ProjectVo projectVo=projectFeignClient.getProjectById(Long.parseLong(sourceId));
                if(projectVo==null){
                    log.info("send discuss comment create notify cancle,project is null");
                    return;
                }
                String repoPath=projectVo.getPathWithNamespace();
                nameSpace.put("path",repoPath);
                nameSpace.put("name",projectVo.getNameWithNamespace());
                url.append("/").append(repoPath);
                List<String> repoMember=projectFeignClient.getRepoMemberIds(sourceId);
                receiver.addAll(repoMember);
        }
        url.append("/discussions/").append(serialNumber);
        notifyParams.setUrl(url.toString());
        notifyParams.setNameSpace(nameSpace);
        notifyParams.setSender(currUserId);
        notifyParams.setTargetId(discussId);
        notifyParams.setVariables(variables);
        //查询此讨论关联人员
        List<String> relateUserId=discussActivityService.selectRelateUser(discussId);
        receiver.addAll(relateUserId);
        if(commentAtsUserIds!=null&&commentAtsUserIds.size()>0){
            receiver.removeAll(commentAtsUserIds);
            commentAtsUserIds.remove(currUserId);//排除自己艾特自己
            //发送艾特通知
            notifyParams.setBusCode(NotifyConstants.DISCUSS_COMMENT_REFER);
            notifyParams.setBatchNum(IdUtil.objectId());
            notifyParams.setReceiver(commentAtsUserIds);
            log.info("----send discuss comment create notify refer params:{}", JSON.toJSONString(notifyParams));
            notifyFeignClient.sendNotify(notifyParams);
        }
        //排除自己
        receiver.remove(currUserId);
        if(receiver.size()>0){
            //移除当前操作人，对本人不通知
            notifyParams.setBusCode(NotifyConstants.DISCUSS_COMMENT_CREATE);
            notifyParams.setBatchNum(IdUtil.objectId());
            notifyParams.setReceiver(receiver);
            log.info("----send discuss comment create notify params:{}", JSON.toJSONString(notifyParams));
            notifyFeignClient.sendNotify(notifyParams);
        }
    }

    @Override
    public void sendDiscussCommentUpdate(CommentUpdateActivityDTO params) {
        String currUserId=params.getActionUid();
        String discussId=params.getDiscussId();
        log.info("send discuss comment update notify begin discussId:{}",discussId);
        DiscussVO discussVO=discussService.selectDiscussById(discussId);
        if(discussVO==null){
            log.info("send discuss comment update notify cancle ,discussId:{} is not exist",discussId);
            return;
        }
        String commentId=params.getCommentId();
        DiscussCommentVO discussCommentVO=discussCommentService.selectDiscussCommentById(commentId);
        if(discussCommentVO==null){
            return;
        }
        Set<String>  commentAtsUserIds=null;
        List<String> commentAts=params.getCommentAts();
        if(commentAts!=null&&commentAts.size()>0){
            //批量查询艾特的人
            commentAtsUserIds=userFeignClient.batchQueryByUsername(commentAts);
        }
        Map<String, String> variables=new HashMap<>();
        String title=discussVO.getTitle();
        variables.put("title",title);
        Integer serialNumber=discussVO.getSerialNumber();
        variables.put("serial_num",serialNumber.toString());
        String desc= HtmlParserTextUtil.htmlParserText(discussCommentVO.getContent(),200);
        variables.put("comment",desc);
        String sourceId=discussVO.getSourceId();
        Integer sourceType=discussVO.getSourceType();
        //查询用户信息
        List<String> userIds=new ArrayList<>();
        userIds.add(currUserId);
        Map<String, UserInfoVO>  userInfoVOMap= userFeignClient.batchQuery(userIds);
        if(userInfoVOMap!=null){
            UserInfoVO userInfoVO=userInfoVOMap.get(currUserId);
            if(userInfoVO!=null){
                variables.put("nick_name",userInfoVO.getNickname());
            }else{
                variables.put("nick_name","账号已注销");
            }
        }
        //根据discussId查询讨论详情
        SendNotifyDTO notifyParams=new SendNotifyDTO();
        JSONObject nameSpace=new JSONObject();
        nameSpace.put("id",sourceId);
        nameSpace.put("type", SourceTypeEnum.get(sourceType).getCode());
        StringBuilder url=new StringBuilder();
        url.append(apiConfigData.getAtomGitHost());
        Set<String> receiver=new HashSet<>();
        switch (sourceType){
            case 1:
                OrgInfoVo orgInfoVo=orgFeignClient.getById(sourceId);
                if(orgInfoVo==null){
                    log.info("send discuss comment update notify cancle,org is null");
                    return;
                }
                String path=orgInfoVo.getNameSpace();
                nameSpace.put("path",path);
                nameSpace.put("name",orgInfoVo.getOrgName());
                url.append("/orgs/").append(path);
                Integer page=1;
                long total=0L;
                List<String> pageList =null;
                do {
                    try {
                        //查询组织成员
                        OrgMemberIdDTO query = new OrgMemberIdDTO();
                        query.setOrgId(sourceId);
                        query.setPageNum(page);
                        query.setPageSize(50);
                        Page<String> pageRes = orgFeignClient.getOrgMemberId(query);
                        pageList = pageRes.getRecords();
                        total=pageRes.getTotal();
                        receiver.addAll(pageList);
                        ++page;
                    }catch (Exception e){
                        e.printStackTrace();
                        log.info("send discuss comment update notify query org member error !");
                    }
                }while (receiver.size()<total);
                break;
            case 2:
                ProjectVo projectVo=projectFeignClient.getProjectById(Long.parseLong(sourceId));
                if(projectVo==null){
                    log.info("send discuss comment update notify cancle,project is null");
                    return;
                }
                String repoPath=projectVo.getPathWithNamespace();
                nameSpace.put("path",repoPath);
                nameSpace.put("name",projectVo.getNameWithNamespace());
                url.append("/").append(repoPath);
                List<String> repoMember=projectFeignClient.getRepoMemberIds(sourceId);
                receiver.addAll(repoMember);
        }
        url.append("/discussions/").append(serialNumber);
        notifyParams.setUrl(url.toString());
        notifyParams.setNameSpace(nameSpace);
        notifyParams.setSender(currUserId);
        notifyParams.setTargetId(discussId);
        notifyParams.setVariables(variables);
        //查询此讨论关联人员
        List<String> relateUserId=discussActivityService.selectRelateUser(discussId);
        receiver.addAll(relateUserId);
        if(commentAtsUserIds!=null&&commentAtsUserIds.size()>0){
            receiver.removeAll(commentAtsUserIds);
            commentAtsUserIds.remove(currUserId);//排除自己艾特自己
            //发送艾特通知
            notifyParams.setBusCode(NotifyConstants.DISCUSS_COMMENT_REFER);
            notifyParams.setBatchNum(IdUtil.objectId());
            notifyParams.setReceiver(commentAtsUserIds);
            log.info("----send discuss comment update notify refer params:{}", JSON.toJSONString(notifyParams));
            notifyFeignClient.sendNotify(notifyParams);
        }
        //排除自己
        receiver.remove(currUserId);
        if(receiver.size()>0){
            //移除当前操作人，对本人不通知
            notifyParams.setBusCode(NotifyConstants.DISCUSS_COMMENT_UPDATE);
            notifyParams.setBatchNum(IdUtil.objectId());
            notifyParams.setReceiver(receiver);
            log.info("----send discuss comment update params:{}", JSON.toJSONString(notifyParams));
            notifyFeignClient.sendNotify(notifyParams);
        }

    }

    @Override
    public void sendDiscussUpdateState(UpdateStateActivityDTO params) {
        String currUserId=params.getActionUid();
        String discussId=params.getDiscussId();
        DiscussStateEnum state=params.getState();
        log.info("send discuss update state notify begin discussId:{}",discussId);
        DiscussVO discussVO=discussService.selectDiscussById(discussId);
        if(discussVO==null){
            log.info("send discuss update state notify cancle ,discussId:{} is not exist",discussId);
            return;
        }
        Map<String, String> variables=new HashMap<>();
        String title=discussVO.getTitle();
        variables.put("title",title);
        Integer serialNumber=discussVO.getSerialNumber();
        variables.put("serial_num",serialNumber.toString());
        variables.put("state_desc",state.getDesc());
        String sourceId=discussVO.getSourceId();
        Integer sourceType=discussVO.getSourceType();
        //查询用户信息
        List<String> userIds=new ArrayList<>();
        userIds.add(currUserId);
        Map<String, UserInfoVO>  userInfoVOMap= userFeignClient.batchQuery(userIds);
        if(userInfoVOMap!=null){
            UserInfoVO userInfoVO=userInfoVOMap.get(currUserId);
            if(userInfoVO!=null){
                variables.put("nick_name",userInfoVO.getNickname());
            }else{
                variables.put("nick_name","账号已注销");
            }
        }
        //根据discussId查询讨论详情
        SendNotifyDTO notifyParams=new SendNotifyDTO();
        JSONObject nameSpace=new JSONObject();
        nameSpace.put("id",sourceId);
        nameSpace.put("type", SourceTypeEnum.get(sourceType).getCode());
        StringBuilder url=new StringBuilder();
        url.append(apiConfigData.getAtomGitHost());
        Set<String> receiver=new HashSet<>();
        switch (sourceType){
            case 1:
                OrgInfoVo orgInfoVo=orgFeignClient.getById(sourceId);
                if(orgInfoVo==null){
                    log.info("send discuss update state notify cancle,org is null");
                    return;
                }
                String path=orgInfoVo.getNameSpace();
                nameSpace.put("path",path);
                nameSpace.put("name",orgInfoVo.getOrgName());
                url.append("/orgs/").append(path);
                Integer page=1;
                long total=0L;
                List<String> pageList =null;
                do {
                    try {
                        //查询组织成员
                        OrgMemberIdDTO query = new OrgMemberIdDTO();
                        query.setOrgId(sourceId);
                        query.setPageNum(page);
                        query.setPageSize(50);
                        Page<String> pageRes = orgFeignClient.getOrgMemberId(query);
                        pageList = pageRes.getRecords();
                        total=pageRes.getTotal();
                        receiver.addAll(pageList);
                        ++page;
                    }catch (Exception e){
                        e.printStackTrace();
                        log.info("send discuss update state notify query org member error !");
                    }
                }while (receiver.size()<total);
                break;
            case 2:
                ProjectVo projectVo=projectFeignClient.getProjectById(Long.parseLong(sourceId));
                if(projectVo==null){
                    log.info("send discuss update state notify cancle,project is null");
                    return;
                }
                String repoPath=projectVo.getPathWithNamespace();
                nameSpace.put("path",repoPath);
                nameSpace.put("name",projectVo.getNameWithNamespace());
                url.append("/").append(repoPath);
                List<String> repoMember=projectFeignClient.getRepoMemberIds(sourceId);
                receiver.addAll(repoMember);
        }
        url.append("/discussions/").append(serialNumber);
        notifyParams.setUrl(url.toString());
        notifyParams.setNameSpace(nameSpace);
        notifyParams.setSender(currUserId);
        notifyParams.setTargetId(discussId);
        notifyParams.setVariables(variables);
        //查询此讨论关联人员
        List<String> relateUserId=discussActivityService.selectRelateUser(discussId);
        receiver.addAll(relateUserId);
        //排除自己
        receiver.remove(currUserId);
        if(receiver.size()>0){
            //移除当前操作人，对本人不通知
            notifyParams.setBusCode(NotifyConstants.DISCUSS_UPDATE_STATE);
            notifyParams.setBatchNum(IdUtil.objectId());
            notifyParams.setReceiver(receiver);
            log.info("----send discuss update state notify params:{}", JSON.toJSONString(notifyParams));
            notifyFeignClient.sendNotify(notifyParams);
        }
    }
}
